// pages/xuanfeng/index.js
Page({
  request(url){
return new Promise((resolve,reject)=>{
  wx.request({
    url,
    success:r=>{
      resolve(r.data)
    },
    fail:e=>{
      reject(null)
    }
  })
})
},
play(e){ //播放点击的视频
  let index=e.currentTarget.dataset.index //点击视频的索引号
  console.log(index)
  wx.navigateTo({//跳转到播放页面去播放视频
    url: '/pages/xuanfeng/player?index='+index
  })
},

	menus(e) {
		console.log(e)
		let menuIndex = e.target.dataset.index
		let oldIndex = this.data.menuIndex
		let change = {
			menuIndex
		}
		change['menus[' + oldIndex + '].selected'] = false
		change['menus[' + menuIndex + '].selected'] = true
		this.setData(change)
	},
	async types(e) { //点击菜单对应的某类型
		let typeIndex = e.target.dataset.index //获取点击类型的索引号
		let menuIndex = this.data.menuIndex //当前菜单的索引号
		let menu = this.data.menus[menuIndex] //获取当前菜单项
		let type = menu.types[typeIndex] //获取当前类型项
		let oldIndex = menu.typeIndex //上次点击的类型索引号
		let change = {} //记录有变化的数据
		if (oldIndex != null) change['menus[' + menuIndex + '].types[' + oldIndex + '].selected'] = false
		change['menus[' + menuIndex + '].types[' + typeIndex + '].selected'] = true
		change['menus[' + menuIndex + '].typeIndex'] = typeIndex
		if(type.list==null){ //当前类型下没有视频数据时才会发起请求
			let url = 'https://miao101.com/tag/' + encodeURIComponent(type.type) + '/1'
			let html= await this.request(url) //获取网页返回的html文本
			if(html!=null){ //成功获取数据
				let regex = /<a href="([^"]+)"><img src="([^"]+)" class=".+?" alt="([^"]+)">/g
				let r, list = [] //用来存储找到的所有电影信息
				while (r = regex.exec(html)) {
					list.push({
						url: r[1], //视频超链接
						img: r[2], //图片地址
						title: r[3], //视频的标题
					})
				}
				change['menus[' + menuIndex + '].types[' + typeIndex + '].list'] = list
			}else{ //获取数据失败，可以给出提醒信息

			}
		}
		this.setData(change)
	},

  onHide(){
    wx.setStorageSync('xuanfeng', this.data)
  },onUnload(){
    wx.setStorageSync('xuanfeng', this.data)
  },onLoad(){
    let data=wx.getStorageSync('xuanfeng')
    if(data)this.setData(data)
  },
	data: {
		menuIndex: 0,
		menus: [{
				menu: '电影',
				selected: true,
				types: [{
						type: "动作片"
					},
					{
						type: "喜剧片"
					},
					{
						type: "爱情片"
					},
					{
						type: "科幻片"
					},
					{
						type: "恐怖片"
					},
					{
						type: "犯罪片"
					},
					{
						type: "动漫片"
					},
					{
						type: "剧情片"
					},
					{
						type: "战争片"
					},
					{
						type: "纪录片"
					},
				]
			},
			{
				menu: '电视剧',
				types:[
					{type:"国产剧"},
					{type:"香港剧"},
					{type:"韩国剧"},
					{type:"欧美剧"},
					{type:"台湾剧"},
					{type:"日本剧"},
					{type:"泰国剧"},
				]
			},
			{
				menu: '综艺',
				types:[
					{type:"大陆综艺"},
					{type:"日韩综艺"},
					{type:"港台综艺"},
					{type:"欧美综艺"},
				]
			},
			{
				menu: '动漫',
				types:[
					{type:"国产动漫"},
					{type:"港台动漫"},
					{type:"日韩动漫"},
					{type:"欧美动漫"},
					{type:"海外动漫"},
				]
			},
			{
				menu: '体育',
				types:[
					{type:"足球"},
					{type:"篮球"},
					{type:"网球"},
					{type:"斯诺克"},
				]
			},
		]
	},

})